package com.etone.smartAudit.service;

import com.etone.daemon.db.helper.QueryHelper;
import com.etone.smartAudit.enums.DataBaseType;
import com.etone.smartAudit.mapper.AuditExceptionForeignReportMapper;
import com.etone.smartAudit.utils.NumberUtil;
import com.etone.smartAudit.utils.ObjectUtil;
import com.etone.smartAudit.utils.database.DatabaseUtil;
import com.etone.smartAudit.utils.database.GreenplumUtil;
import com.etone.smartAudit.utils.word.ForeignWordTemplateUtil;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.auditExceptionCityReport.AuditExceptionCityReportHeaderVO;
import com.etone.smartAudit.vo.auditExceptionCityReport.AuditExceptionCityReportVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: sovy
 * @date: 2021/1/29
 * @description:
 */
@Service
public class AuditExceptionForeignReportService {

    @Value("${spring.datasource.database}")
    private String database;

    @Value("${spring.gpds.schema}")
    private String gpSchema;

    @Value("${spring.gpds.database}")
    private String gpDatabase;

    @Value("${spring.gpds.ip}")
    private String gpIp;

    @Value("${spring.gpds.port}")
    private Integer gpPort;

    @Value("${spring.gpds.username}")
    private String gpUsername;

    @Value("${spring.gpds.password}")
    private String gpPassword;

    @Autowired
    private AuditExceptionForeignReportMapper auditExceptionForeignReportMapper;

    /**
     * 获取地市
     *
     * @return
     */
    public List<String> findAllCity() {
        return auditExceptionForeignReportMapper.findAllCity();
    }

    /**
     * 获取列表
     *
     * @param cityName
     * @param page
     * @param pageSize
     * @return
     */
    public AuditExceptionCityReportVO list(String cityName, int page, int pageSize) {
        List<AuditExceptionCityReportHeaderVO> tableColumn = auditExceptionForeignReportMapper.findTableColumn(database);
        Page<Map<String, Object>> dataPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> auditExceptionForeignReportMapper.findList(null, cityName, null));
        return AuditExceptionCityReportVO.builder().head(tableColumn).data(PagerVO.of(dataPage)).build();
    }

    /**
     * 导出报告
     *
     * @param ids
     * @param fields
     * @param specialKeys
     * @param startDate
     * @param endDate
     * @param response
     */
    public void exportReport(List<Integer> ids, List<String> fields, List<String> specialKeys, String startDate, String endDate, HttpServletResponse response) {
        List<Map<String, Object>> datas = auditExceptionForeignReportMapper.findList(fields, null, ids);
        for (Map<String, Object> data : datas) {
            for (String specialKey : specialKeys) {
                data.put(specialKey, "");
            }
            data.put("reportStartTime", startDate.replaceAll("-", "年") + "月");
            data.put("reportEndTime", endDate.replaceAll("-", "年") + "月");
        }

        ForeignWordTemplateUtil.export(datas, response);
    }



}
